<template>
  <div>
    <div class="layout">
      <div>
        <BackNav left-text="返回" left-arrow title="安全中心"></BackNav>
      </div>
      <div class="top"></div>
      <div class="wrapper">
        <div class="main">
          <div class="list-item">
            <van-cell is-link @click="showPopup">修改密码</van-cell>
            <van-popup
              v-model="show"
              closeable
              position="bottom"
              :style="{ height: '40%' }"
            >
              <div class="form-box">
                <div class="text">修改密码</div>
                <van-form @submit="mcode">
                  <van-field
                    v-model="userInfo.oldpassword"
                    :type="isopen ? 'text' : 'password'"
                    name="oldpassword"
                    label="旧密码"
                    placeholder="旧密码(首字母必须为字母)"
                    autocomplete="off"
                    :right-icon="isopen ? 'eye-o' : 'closed-eye'"
                    @click-right-icon="togglePasswordStatus"
                    :rules="[
                      { required: true, message: '请填写密码' },
                      {
                        pattern: passwordReg,
                        message: '支持数字字母下划线组合16位并且以字母开头',
                      },
                    ]"
                  />
                  <van-field
                    v-model="userInfo.newpassword"
                    :type="open ? 'text' : 'password'"
                    name="newpassword"
                    label="新密码"
                    placeholder="新密码(首字母必须为字母)"
                    autocomplete="off"
                    :right-icon="open ? 'eye-o' : 'closed-eye'"
                    @click-right-icon="togglePasswordStatuss"
                    :rules="[
                      { required: true, message: '请填写密码' },
                      {
                        pattern: passwordReg,
                        message: '支持数字字母下划线组合16位并且以字母开头',
                      },
                    ]"
                  />
                  <div class="login-btn">
                    <van-button round block color="#0c34ba" native-type="submit"
                      >提&nbsp;&nbsp;&nbsp;交</van-button
                    >
                  </div>
                </van-form>
              </div>
            </van-popup>
          </div>
          <div class="list-item">
            <van-cell is-link @click="cancellation"> 注销账号 </van-cell>
          </div>
        </div>
      </div>
      <div style="margin: 100px 16px" @click="unlogin">
        <van-button round block type="info" native-type="submit" color="#0c43ba"
          >退出登录</van-button
        >
      </div>
    </div>
  </div>
</template>
<script>
import BackNav from "../components/BackNav";
import { Dialog } from "vant";
export default {
  components: {
    BackNav,
  },
  data() {
    return {
      show: false,
      userInfo: {
        oldpassword: "",
        newpassword: "",
      },
      isopen: false,
      open: false,
      //   验证密码（数字字母下划线组合并且以字母开头）
      passwordReg: /^[A-Za-z]\w{5,15}$/,
    };
  },
  methods: {
    // 修改密码弹出层
    showPopup() {
      this.show = true;
    },
    // 注销账号弹出层
    cancellation() {
      Dialog.confirm({
        title: "注销账号",
        message: "是否确定注销账号，一旦注销无法恢复",
      })
        .then(() => {
          this.axios({
            method: "post",
            url: "/destroyAccount",
          })
            .then((res) => {
              
               if(res.data.code=='G001'){
                this.$toast(res.data.msg);
                setTimeout(() => {
                  this.$router.push({name:"Register"});
                }, 500);
              }
            })
            .catch((err) => {
              
            });
        })
        .catch(() => {
          
        });
    },
    // 切换开闭眼
    togglePasswordStatus() {
      this.isopen = !this.isopen;
    },
    // 切换开闭眼
    togglePasswordStatuss() {
      this.open = !this.open;
    },
    // 修改密码
    mcode() {
      this.axios({
        method: "post",
        url: "/updatePassword",
        data: {
          password: this.userInfo.newpassword,
          oldPassword: this.userInfo.oldpassword,
        },
      })
        .then((res) => {
          
          if (res.data.code == "E001") {
            this.$toast(res.data.msg);
            setTimeout(() => {
              this.$router.push({ name: "Login" });
            }, 500);
          }
        })
        .catch((err) => {
          
        });
    },
    // 退出登录
    unlogin() {
      this.axios({
        method: "post",
        url: "/logout",
      })
        .then((res) => {
          
          if(res.data.code=='F001'){
            sessionStorage.setItem('token','');
            setTimeout(() => {
              this.$router.push({name:'Login'});
            }, 1000);
          }
        })
        .catch((err) => {
          
        });
    },
  },
};
</script>
<style lang="less" scoped>
.middle {
  vertical-align: middle;
}
.layout {
  background: #f0f0f0;
  position: fixed;
  top: 0;
  bottom: 0;
  right: 0;
  left: 0;
  .top {
    height: 100px;
    background: #0c43ba;
  }
  .wrapper {
    width: 90%;
    background: #fff;
    position: relative;
    top: -20px;
    border-radius: 10px;
    margin: 0 auto;
    .list-item {
      border-bottom: 1px dashed #f0f0f0;
      padding: 10px 0;
      display: flex;
      align-items: center;
      justify-content: space-between;
      flex-wrap: wrap;
      &:last-child {
        border-bottom: 0;
      }
      .form-box {
        margin-top: 20px;
        padding: 0 20px;
        .text {
          font-size: 16px;
          margin-bottom: 20px;
        }
        .login-btn {
          margin: 50px auto;
        }
      }
    }
  }
}
</style>